package com.dwin.dingnuo.views;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.dwin.dingnuo.R;
import com.dwin.dingnuo.utils.ToastUtil;


/**
 * 语言选择 arr_language
 */
public class ChooseLanguageDialog implements OnClickListener {
    private Dialog mDialog = null;
    private Activity mActivity = null;

    ClickBack clickBack;

    public ChooseLanguageDialog(Activity act) {
        mActivity = act;
    }

    /**
     * 显示设置头像选择对话框
     */
    public void popSelectDialog(String language) {
        setDialog(language);
        mDialog.show();
    }

    /**
     * 设置dialog窗口样式 布局 点击事件
     */
    private void setDialog(String language) {
        // 此处直接new一个Dialog对象出来，在实例化的时候传入主题
        if (mDialog == null) {
            mDialog = new Dialog(mActivity, R.style.DialogTheme);
            mDialog.setContentView(R.layout.language_choice);
            mDialog.setCanceledOnTouchOutside(true);
            Window window = mDialog.getWindow();
            window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            window.setGravity(Gravity.BOTTOM);
            window.setWindowAnimations(R.style.dialog_style);
            window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            ListView lv = mDialog.findViewById(R.id.lv_language);
            ChooseLanguageAdapter adapter = new ChooseLanguageAdapter(mActivity, language);
            lv.setAdapter(adapter);
            TextView cancelTxt = mDialog.findViewById(R.id.select_cancel);
            cancelTxt.setOnClickListener(this);

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (clickBack != null) {
                        mDialog.dismiss();
                        clickBack.selectItem(position);
                    }
                }
            });
        }
    }

    /**
     * 显示提示信息
     *
     * @param msg 信息
     */
    public void showToast(String msg) {
        ToastUtil.toastShort((Context) mActivity, msg);
    }

    /**
     * 处理各控件点击事件
     */
    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        if (viewId == R.id.select_cancel) {
            mDialog.dismiss();
        }
    }


    public void setCallBack(ClickBack clickBack) {
        this.clickBack = clickBack;
    }

    /**
     * 开始拍照
     */
    public void startCamera() {
    }

    /**
     * 打开相册 选择照片
     */
    public void startAlbum() {
    }


    public interface ClickBack {
        public void selectItem(int position);
    }
}
